package App;

use warnings;
use strict;
use CGI::Cookie;
use CGI::Session;
use Config::Any;
use Email::Stuff;
use Model::Schema;
use View::Template;

=head1 NAME

App - This module loads all third party modules and provides accessors to the calling module!
By default, this module has accessors to include configuration, template, email, database and session objects and
is were the developer should place custom add-on accessors and additional code.

=cut

sub new {
    my ($class, $base)     = @_;
    my $self               = bless {}, $class;
       
       #///////////////////
       #load modules using the following procedure, they will be available to the application as $s->nameofobject e.g. $s->config
       $base->plug( 'cookie',   sub { my $self = shift; my @p = @_; my @c = $self->application->{cookie_data} ? @{$self->application->{cookie_data}} : (); my $o = new CGI::Cookie(@p); push @c, $o; $self->{store}->{application}->{cookie_data} = \@c; return $o } ); # build cookie accessor
       $base->plug( 'session',  sub { my $self = shift; CGI::Session->name("SPSID"); new CGI::Session("driver:file", undef, {Directory=>$self->application->{path}.'/tmp'}) } ); # load configuration file
       $base->plug( 'config',   sub { my $self = shift; return Config::Any->load_files({files=>['.conf'], use_ext=>1, flatten_to_hash => 1}) } ); # load configuration file
       $base->plug( 'template', sub { my $self = shift; return View::Template->new($self) } ); # build template accessor
       $base->plug( 'database', sub { my $self = shift; return Model::Schema->initialize($self); } ); # build database accessor
       $base->plug( 'email',    sub { my $self = shift; my $email = Email::Stuff->new; $email->from($self->conf->{email}->{me}); $email->to($self->conf->{email}->{me}); return $email; } ); # build email accessor
       
       #///////////////////
       #USER-DEFINED MODULE PLUGINS HERE...
       #$base->plug(...)
       
    return $self;
}

1; # End of App
